package com.person.jc.service.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.common.Constants;
import com.person.jc.common.SQLSpell;
import com.person.jc.service.Service;
import com.person.jc.utils.Utils;

public class BsRepairConfirmService extends Service {
	
	public static BsRepairConfirmService me = new BsRepairConfirmService();
	private static String optMenu = "维修确认";
	
	public List<Record> getList(Map<String, Object> map) {
		String temp = "";
		List<Object> params = new ArrayList<Object>();
		String tempSql = "SELECT o.service_user,\n" +
						"		("+SQLSpell.userName("o.service_user")+") service_user_,\n" +
						"       o.cust_id,o.order_id,\n" + 
						"		("+SQLSpell.customerName("o.cust_id")+") cust_id_,\n" +
						"       ta.leader_id,\n" + 
						"		("+SQLSpell.userName("ta.leader_id")+") opt_user_,\n" +
						"       COUNT(rl.log_id) num1,\n" + 
						"       sum(IFNULL(rc.zfNum, 0)) zfNum,\n" + 
						"       sum(IFNULL(rc.wxNum, 0)) wxNum\n" + 
						"  FROM bs_repair_reg_log rl\n" + 
						"  LEFT JOIN bs_order_equipment oe ON rl.equ_id = oe.equ_id\n" + 
						"  LEFT JOIN bs_order o ON rl.order_id = o.order_id\n" + 
						"  LEFT JOIN bs_task_assign ta ON rl.order_id = ta.order_id and ta.order_type = 2\n" + 
						"  LEFT JOIN (SELECT t.log_id, t.opt_user, SUM(IF(t.confirm_status = 0, 1, 0)) zfNum, SUM(IF(t.confirm_status = 1, 1, 0)) wxNum FROM bs_repair_confirm_log t GROUP BY t.opt_user, t.log_id) rc ON rc.log_id = rl.log_id AND rc.opt_user = o.service_user\n" + 
						" WHERE o.order_type = ? and o.flow_id = "+Constants.FLOW_WX_JCRW+"\n";
		StringBuffer sql = new StringBuffer(tempSql);
		params.add(map.get("orderType"));
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("serviceUser")))){
			sql.append("AND o.service_user = ?\n");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("custId")))){
			sql.append("AND o.cust_id = ?\n");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("orderId")))){
			sql.append("AND o.order_id like ?\n");
			params.add("%"+temp+"%");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("optUser")))){
			sql.append("AND rl.opt_user = ?\n");
			params.add(temp);
		}
		sql.append("GROUP BY o.order_id\n");
		sql.append("ORDER BY o.order_id");
		return Db.find(sql.toString(), Utils.getObjParam(params));
	}
	
	public void save(String logIds, Object[][] params, int length){
		String sql = "INSERT INTO bs_repair_confirm_log (log_id, confirm_status, opt_user, opt_date) VALUES (?, ?, ?, now())";
		Db.batch(sql, params, length);
		super.setCRUD(optMenu, "C", "新增维修确认：" + logIds);
	}

	public void delete(String[] logId) {
		String temp = "";
		if(logId!=null && logId.length>0){
			for (int i = 0; i < logId.length; i++) {
				if(temp!=""){
					temp += ",";
				}
				temp += logId[i];
			}
		}
		String sql = "delete from bs_repair_confirm_log where log_id IN ("+temp+")";
		Db.update(sql);
	}

	//维修确认，变更订单FLOW
	public void changeOrderFlow(String[] logId) {
		if(logId!=null && logId.length>0){
			String sql = "select DISTINCT order_id from bs_repair_reg_log where log_id in ("+Utils.toString(logId)+")";
			
			String subSQL = "select count(oe.equ_id) from bs_order_equipment oe \n" +
					"left join (select b.* from bs_repair_confirm_log a left join bs_repair_reg_log b on a.log_id = b.log_id) rl \n" +
					"on oe.order_id = rl.order_id and oe.equ_id = rl.equ_id\n" +
					"where oe.order_id = ? \n" +
					"and rl.log_id is null";
			List<Object> orders = Db.query(sql);
			for (Object o : orders) {
				Long num = Db.queryLong(subSQL, o);
				if(num.intValue() == 0){
					BsOrderService.me.updateFlow(String.valueOf(o), Constants.FLOW_WX_WXQR);
				}
			}
		}
	}

	public void resetParts(String logIds) {
		String sql = "update bd_device_parts set parts_status = 0 where parts_id in (SELECT parts_id FROM bs_repair_check_detail where log_id in ("+logIds+"))";
		Db.update(sql);
		super.setCRUD(optMenu, "U", "修改设备配件状态，检查编号："+logIds);
	}

}
